#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright [Gtlions Lai].
# Create Date:
# Update Date:
__authors__ = '"Gtlions Lai" <gtlions.l@qq.com>'
"""Function for GreenPlum DB tables partitions manager - add partitions.

Drop tables partitions.

  ClassFoo: None.
  functionBar(): None.
"""

import sys
import platform
import colorama
import dsn

if platform.system() == 'Windows' or platform.system() == 'Darwin':
    from psycopg2 import *
elif platform.system() == 'Linux':
    from pygresql.pgdb import *
import datetime
import partitioncom

begin_time = datetime.datetime.now()
try:
    db = connect(host=dsn.host, database=dsn.database, user=dsn.user, password=dsn.password)
except Exception, e:
    print 'DATABASE Connection Error,Pls Check the db info:[' + dsn.host, dsn.database, dsn.user + ']'
    sys.exit(1)
try:
    cur = db.cursor()
    print "#" * 80
    print "#", (colorama.Fore.RED + "GPDB Partition Manager(-PD) For BOMC".center(76)), (colorama.Fore.RESET + "#")
    print "#", (colorama.Fore.GREEN + "Processing".center(76)), (colorama.Fore.RESET + "#")
    print "#" * 80
    print "+" + "-" * 26 + "+" + "-" * 5 + "+" + "-" * 45 + "+"
    print "|" + (colorama.Fore.YELLOW + "Timestapm".center(26)) + (colorama.Fore.RESET + "|") + (
        colorama.Fore.YELLOW + "Seq".center(5)) + (colorama.Fore.RESET + "|") + (
              colorama.Fore.YELLOW + "Relation".center(45)) + (colorama.Fore.RESET + "|")
    print "+" + "-" * 26 + "+" + "-" * 5 + "+" + "-" * 45 + "+"
    partitioncom.partition_drop()
    print "+" + "-" * 26 + "+" + "-" * 5 + "+" + "-" * 45 + "+"

    end_time = datetime.datetime.now()
    cur.execute("select gpmg.func_log(%s,%s,%s,%s,%s,%s)", ("-PD", begin_time, end_time, 0, "Done.", 0))
    cur.close()
    db.commit()
    db.close()
except Exception, e:
    print 'error'
